<template>
	<view>
		<u-popup v-model="showPicker" mode="bottom" border-radius="38" :closeable="true" close-icon-pos="top-left"
		 close-icon-color="#000" width="750rpx" height="85vh">
			<view class="scrollContent" style="padding-top: 80rpx;height: 85vh;">
				<scroll-view scroll-y="true" @scroll="scrollPopup" style="height: 85vh;">
					<u-index-list :scrollTop="scrollTop" offset-top="80">
						<view v-for="(item, index) in countryCode" :key="index">
							<u-index-anchor :index="item.title" />
							<view class="list-cell" v-for="(item1,index1) in item.countries" :key="index1" @click="selectyCode(item1)">
								<text>{{item1.name}}</text>
								<text style="color: #808080;">{{item1.countryCode}}</text>
							</view>
						</view>
					</u-index-list>
				</scroll-view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import countryCode from '@/static/data/countryCode.js';
	export default {
		props: {
			transmit: {
				type: Function,
				default: null
			}
		},
		data() {
			return {
				scrollTop: 0,
				indexList: [],
				showPicker: false,
				countryCode: []
			};
		},
		created() {
			this.getCountryCode();
		},
		methods: {
			scrollPopup(e) {
				this.scrollTop = e.detail.scrollTop
			},
			open() {
				this.showPicker = true
			},
			close() {
				this.showPicker = false
			},
			selectyCode(item) {
				this.transmit(item);
				this.close();
			},
			getCountryCode() {
				for (let i = 0; i < 26; i++) {
					this.countryCode[i] = {
						title: String.fromCharCode(65 + i),
						countries: []
					};
					countryCode.forEach((item, index) => {
						if (item.pinyin.substr(0, 1) === String.fromCharCode(65 + i).toLowerCase()) {
							this.countryCode[i].countries.push({
								tel: item.tel,
								countryCode: '+' + item.tel,
								name: item.name
							});
						}
					});
				}
				this.indexList = this.countryCode.map(res => (res.title))
			}
		},
	};
</script>

<style lang="scss">
	/deep/.u-close {
		&::after {
			content: '选择国家和地区';
			margin-left: 10px;
		}
	}

	/deep/.u-index-bar__sidebar {
		top: 55.2%;
	}

	/deep/.u-index-bar__index {
		padding: 3.5px 9px;
	}

	.list-cell {
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
		width: 100%;
		padding: 10px 75rpx 10px 24rpx;
		overflow: hidden;
		color: #323233;
		font-size: 14px;
		line-height: 24px;
		background-color: #fff;

		&:active {
			background-color: rgba($color: #ccc, $alpha: 0.3);
		}
	}
</style>
